package com.shaoshaohuo.app.ui.fragment;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.shaoshaohuo.app.R;
import com.shaoshaohuo.app.adapter.HeaderListAdapter;
import com.shaoshaohuo.app.constant.ExtraName;
import com.shaoshaohuo.app.constant.ListViewAction;
import com.shaoshaohuo.app.entity.AdModel;
import com.shaoshaohuo.app.entity.BaseEntity;
import com.shaoshaohuo.app.entity.Car;
import com.shaoshaohuo.app.entity.CarListEntity;
import com.shaoshaohuo.app.entity.Cargo;
import com.shaoshaohuo.app.entity.CargoListEntity;
import com.shaoshaohuo.app.entity.FocusImage;
import com.shaoshaohuo.app.entity.MainEntity;
import com.shaoshaohuo.app.entity.SearchCar;
import com.shaoshaohuo.app.entity.SearchGoods;
import com.shaoshaohuo.app.framework.AccountManager;
import com.shaoshaohuo.app.framework.SharedPreferencesHelper;
import com.shaoshaohuo.app.net.HttpRequest.RequestListener;
import com.shaoshaohuo.app.net.RequestService;
import com.shaoshaohuo.app.ui.FindGoodsActivity;
import com.shaoshaohuo.app.ui.GrabOrderActivity;
import com.shaoshaohuo.app.ui.LoginActivity;
import com.shaoshaohuo.app.ui.MainTabActivity;
import com.shaoshaohuo.app.ui.SearchCarActivity;
import com.shaoshaohuo.app.ui.SearchGoodsActivity;
import com.shaoshaohuo.app.ui.ToosActivity;
import com.shaoshaohuo.app.ui.ec.MyFragmentActivity;
import com.shaoshaohuo.app.ui.shipper.CarOwnerInfoActivity;
import com.shaoshaohuo.app.ui.shipper.FindCarActivity;
import com.shaoshaohuo.app.ui.shipper.ReleaseCarActivity;
import com.shaoshaohuo.app.ui.shipper.SendGoodsActivity;
import com.shaoshaohuo.app.utils.DeviceHelper;
import com.shaoshaohuo.app.utils.ToastUtil;
import com.shaoshaohuo.app.utils.im.IMFactory;
import com.shaoshaohuo.app.view.AdViewPager;
import com.shaoshaohuo.app.view.TopbarView;
import com.shaoshaohuo.app.view.XListView;
import com.umeng.analytics.MobclickAgent;

import java.util.ArrayList;
import java.util.List;

/**
 * 测试内容
 *
 * @version 1.0
 * @author: yusonglin
 * @类 说 明:车主首页
 * @创建时间：2015年7月27日 下午2:13:10
 */
public class CarFragment extends Fragment implements OnClickListener, XListView.IXListViewListener, AdapterView.OnItemClickListener {

    public static int CAR = 0;
    public static int GOODS = 1;

    private int listType; //当前listview  显示的类型
    private boolean carSearch = false;
    private boolean goodsSearch = false;// 标识 listview 的两个状态列表 是否有筛选条件

    private View mContentView;
    private View headerView;

    private TopbarView mTopbarView;
    private AdViewPager adViewPager;

    private View layout_zhaoche;
    private View layout_zhaohuo;
    private View layout_fabuche;
    private View layout_fabuhuo;
    private View layout_zhushou;
    private View layout_kefu;

    private XListView headerListView;
    private HeaderListAdapter headerListAdapter;

    private View tabView;
    private TextView tv_tab_huoyuan;
    private View view_baseline_right;
    private View view_baseline_left;
    private TextView tv_tab_cheyuan;
    private TextView tv_search;
    private RelativeLayout rl_search;
    private TextView tv_send_ad;
    private TextView tv_end_ad;
    private TextView tv_search_detail;
    private ImageView iv_cancel;


    private View tab_gone_view;
    private TextView tv_tab_gone_huoyuan;
    private View view_baseline_gone_right;
    private View view_baseline_gone_left;
    private TextView tv_tab_gone_cheyuan;
    private TextView tv_gone_search;
    private RelativeLayout rl_gone_search;
    private TextView tv_send_gone_ad;
    private TextView tv_end_gone_ad;
    private TextView tv_search_gone_detail;
    private ImageView iv_gone_cancel;

    private String lng = "";
    private String lat = "";
    private int size = 10;

    private String carCursor = "0";
    private List<Car> carList = new ArrayList<>();

    private String goodsCursor = "0";
    private List<Cargo> goodsList = new ArrayList<>();

    private SearchGoods searchGoods = new SearchGoods();
    private SearchCar searchCar = new SearchCar();


    private String mArgument;
    public static final String ARGUMENT = "argument";

    public static CarFragment newInstance(String argument) {
        Bundle bundle = new Bundle();
        bundle.putString(ARGUMENT, argument);
        CarFragment contentFragment = new CarFragment();
        contentFragment.setArguments(bundle);
        return contentFragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle bundle = getArguments();
        if (bundle != null)
            mArgument = bundle.getString(ARGUMENT);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        listType = SharedPreferencesHelper.getInt(SharedPreferencesHelper.Field.LIST_TYPE, GOODS);


        if (mContentView == null) {
            mContentView = inflater.inflate(R.layout.activity_main, null);
            headerView = inflater.inflate(R.layout.view_car_main_header, null);
            tabView = inflater.inflate(R.layout.view_listview_tab, null);

            lng = SharedPreferencesHelper.getString(SharedPreferencesHelper.Field.LNG, "");
            lat = SharedPreferencesHelper.getString(SharedPreferencesHelper.Field.LAT, "");

            initView();
            setUpView();
            initLayout();
            requestMainImages();
            requestGoods(true);
            requestCar(true);
        } else {
            ViewGroup parent = (ViewGroup) mContentView.getParent();
            if (parent != null) {
                parent.removeView(mContentView);
            }


        }
        return mContentView;
    }


    private void initView() {
        mTopbarView = (TopbarView) mContentView.findViewById(R.id.topbar);
        mTopbarView.setCenterModel(TopbarView.ID_TITLE_INDEX_SHIPPER);
        if (getActivity() instanceof MainTabActivity) {
            mTopbarView.setLeftImage(R.drawable.navbar_menu, "");
            mTopbarView.setLeftClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    ((MainTabActivity) getActivity()).toggle();
                }
            });
        } else if (getActivity() instanceof MyFragmentActivity) {
            mTopbarView.setLeftView(true, true);
        }
        if (IMFactory.getUnreadCount() > 0) {
            mTopbarView.setRightImage(R.drawable.icon_message_unread_ad);
        } else {
            mTopbarView.setRightImage(R.drawable.icon_message_ad);
        }
        mTopbarView.setRightClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                if (AccountManager.isLogin()) {
//                    Intent intent = new Intent(getActivity(), MessageCenterActivity.class);
//                    getActivity().startActivity(intent);
                    IMFactory.openConversation(getActivity());
                } else {
                    Intent intent = new Intent(getActivity(), LoginActivity.class);
                    getActivity().startActivity(intent);
                }
            }
        });


        headerListView = (XListView) mContentView.findViewById(R.id.xlv_listview);
        headerListView.addHeaderView(headerView);

        adViewPager = (AdViewPager) mContentView.findViewById(R.id.ec_home_ad_viewpager);


        layout_zhaoche = mContentView.findViewById(R.id.layout_zhaoche);
        layout_zhaohuo = mContentView.findViewById(R.id.layout_zhaohuo);
        layout_fabuche = mContentView.findViewById(R.id.layout_fabuche);
        layout_fabuhuo = mContentView.findViewById(R.id.layout_fabuhuo);
        layout_zhushou = mContentView.findViewById(R.id.layout_zhushou);
        layout_kefu = mContentView.findViewById(R.id.layout_kefu);


        tv_tab_huoyuan = (TextView) tabView.findViewById(R.id.tv_tab_huoyuan);
        tv_tab_cheyuan = (TextView) tabView.findViewById(R.id.tv_tab_cheyuan);
        view_baseline_left = tabView.findViewById(R.id.view_baseline_left);
        view_baseline_right = tabView.findViewById(R.id.view_baseline_right);
        tv_search = (TextView) tabView.findViewById(R.id.tv_search);
        rl_search = (RelativeLayout) tabView.findViewById(R.id.rl_search);
        tv_send_ad = (TextView) tabView.findViewById(R.id.tv_send_ad);
        tv_end_ad = (TextView) tabView.findViewById(R.id.tv_end_ad);
        tv_search_detail = (TextView) tabView.findViewById(R.id.tv_search_detail);
        iv_cancel = (ImageView) tabView.findViewById(R.id.iv_cancel);


        tab_gone_view = mContentView.findViewById(R.id.tab_gone_view);
        tv_tab_gone_huoyuan = (TextView) tab_gone_view.findViewById(R.id.tv_tab_huoyuan);
        tv_tab_gone_cheyuan = (TextView) tab_gone_view.findViewById(R.id.tv_tab_cheyuan);
        view_baseline_gone_left = tab_gone_view.findViewById(R.id.view_baseline_left);
        view_baseline_gone_right = tab_gone_view.findViewById(R.id.view_baseline_right);
        tv_gone_search = (TextView) tab_gone_view.findViewById(R.id.tv_search);
        rl_gone_search = (RelativeLayout) tab_gone_view.findViewById(R.id.rl_search);
        tv_send_gone_ad = (TextView) tab_gone_view.findViewById(R.id.tv_send_ad);
        tv_end_gone_ad = (TextView) tab_gone_view.findViewById(R.id.tv_end_ad);
        tv_search_gone_detail = (TextView) tab_gone_view.findViewById(R.id.tv_search_detail);
        iv_gone_cancel = (ImageView) tab_gone_view.findViewById(R.id.iv_cancel);

        initTabState();
    }

    private void setUpView() {
        setAdapter();
        headerListView.setPullLoadEnable(true);
        headerListView.setPullRefreshEnable(false);
        headerListView.setXListViewListener(this);
        headerListView.setOnItemClickListener(this);
        headerListView.setOnScrollListener(new AbsListView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState) {
            }

            @Override
            public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
                if (firstVisibleItem >= 2) {
                    tab_gone_view.setVisibility(View.VISIBLE);
                } else {
                    tab_gone_view.setVisibility(View.GONE);
                }
            }
        });

        layout_zhaoche.setOnClickListener(this);
        layout_zhaohuo.setOnClickListener(this);
        layout_fabuche.setOnClickListener(this);
        layout_fabuhuo.setOnClickListener(this);
        layout_zhushou.setOnClickListener(this);
        layout_kefu.setOnClickListener(this);

        tv_tab_huoyuan.setOnClickListener(this);
        tv_tab_cheyuan.setOnClickListener(this);
        tv_search.setOnClickListener(this);
        iv_cancel.setOnClickListener(this);
        rl_search.setOnClickListener(this);

        tab_gone_view.setVisibility(View.GONE);
        tv_tab_gone_huoyuan.setOnClickListener(this);
        tv_tab_gone_cheyuan.setOnClickListener(this);
        tv_gone_search.setOnClickListener(this);
        iv_gone_cancel.setOnClickListener(this);
        rl_gone_search.setOnClickListener(this);
    }

    private void initLayout() {
        int screenWidth = DeviceHelper.getScreenWidth(getActivity());
        int width = screenWidth / 4;
        ViewGroup.LayoutParams layoutParams = layout_zhaoche.getLayoutParams();
        layoutParams.width = width;
        layout_zhaoche.setLayoutParams(layoutParams);
        layout_zhaohuo.setLayoutParams(layoutParams);
        layout_fabuche.setLayoutParams(layoutParams);
        layout_fabuhuo.setLayoutParams(layoutParams);
        layout_zhushou.setLayoutParams(layoutParams);
        layout_kefu.setLayoutParams(layoutParams);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.layout_zhaoche:
                Intent intent1 = new Intent(getActivity(), FindCarActivity.class);
                getActivity().startActivity(intent1);
                break;
            case R.id.layout_zhaohuo:
                Intent intent = new Intent(getActivity(), FindGoodsActivity.class);
                getActivity().startActivity(intent);
                break;
            case R.id.layout_fabuche:
                Intent intent5 = new Intent(getActivity(), ReleaseCarActivity.class);
                getActivity().startActivity(intent5);
                break;
            case R.id.layout_fabuhuo:
                Intent intent3 = new Intent(getActivity(), SendGoodsActivity.class);
                getActivity().startActivity(intent3);
                break;
            case R.id.layout_zhushou:
                Intent intent4 = new Intent(getActivity(), ToosActivity.class);
                getActivity().startActivity(intent4);
                break;
            case R.id.layout_kefu:
                IMFactory.openChatServiceContact(getActivity());
                break;
            case R.id.tv_tab_huoyuan:
                changeListType(GOODS);
                break;
            case R.id.tv_tab_cheyuan:
                changeListType(CAR);
                break;
            case R.id.rl_search:
            case R.id.tv_search:
                if (listType == GOODS) {
                    Intent intent7 = new Intent(getActivity(), SearchGoodsActivity.class);
                    intent7.putExtra(ExtraName.searchGoods, searchGoods);
                    getActivity().startActivityForResult(intent7, GOODS);
                } else {
                    Intent intent6 = new Intent(getActivity(), SearchCarActivity.class);
                    intent6.putExtra(ExtraName.searchCar, searchCar);
                    getActivity().startActivityForResult(intent6, CAR);
                }
                break;
            case R.id.iv_cancel:
                cancelSearch();
                break;
        }
    }

    private void cancelSearch() {
        if (listType == GOODS) {
            searchGoods = new SearchGoods();
            goodsSearch = false;
            searchGoodsFinish();
        } else {
            searchCar = new SearchCar();
            carSearch = false;
            searchCarFinish();
        }
        initSearchDetail();
    }


    private void changeListType(int type) {
        if (listType == type) return;
        listType = type;
        SharedPreferencesHelper.setInt(SharedPreferencesHelper.Field.LIST_TYPE, type);
        initTabState();
        headerListView.setPullLoadEnable(true);
        setAdapter();
        if (View.VISIBLE == tab_gone_view.getVisibility())
            headerListView.setSelection(2);
    }

    private void initTabState() {
        if (listType == GOODS) {
            tv_tab_huoyuan.setEnabled(false);
            tv_tab_cheyuan.setEnabled(true);
            view_baseline_left.setVisibility(View.VISIBLE);
            view_baseline_right.setVisibility(View.INVISIBLE);

            tv_tab_gone_huoyuan.setEnabled(false);
            tv_tab_gone_cheyuan.setEnabled(true);
            view_baseline_gone_left.setVisibility(View.VISIBLE);
            view_baseline_gone_right.setVisibility(View.INVISIBLE);

            tv_search.setText("输入精准查找货源");
        } else {
            tv_tab_huoyuan.setEnabled(true);
            tv_tab_cheyuan.setEnabled(false);
            view_baseline_left.setVisibility(View.INVISIBLE);
            view_baseline_right.setVisibility(View.VISIBLE);

            tv_tab_gone_huoyuan.setEnabled(true);
            tv_tab_gone_cheyuan.setEnabled(false);
            view_baseline_gone_left.setVisibility(View.INVISIBLE);
            view_baseline_gone_right.setVisibility(View.VISIBLE);

            tv_search.setText("输入精准查找车源");
        }

        initSearchDetail();
    }

    private void initSearchDetail() {
        if (listType == GOODS) {
            if (goodsSearch) {
                if (TextUtils.isEmpty(searchGoods.origincityid)) {
                    tv_send_ad.setText("全国");
                    tv_send_gone_ad.setText("全国");
                } else {
                    tv_send_ad.setText(searchGoods.originName);
                    tv_send_gone_ad.setText(searchGoods.originName);
                }
                if (TextUtils.isEmpty(searchGoods.receivecityid)) {
                    tv_end_ad.setText("全国");
                    tv_end_gone_ad.setText("全国");
                } else {
                    tv_end_ad.setText(searchGoods.receiveName);
                    tv_end_gone_ad.setText(searchGoods.receiveName);
                }
                StringBuilder stringBuilder = new StringBuilder();
                if (searchGoods.shipptype != 0)
                    stringBuilder.append(searchGoods.shippName + ",");
                if (searchGoods.ranktype != 0)
                    stringBuilder.append(searchGoods.rankName + ",");
                String str = stringBuilder.toString();
                if (str.endsWith(",")) {
                    str.substring(0, str.length() - 1);
                }
                tv_search_detail.setText(str);
                tv_search_gone_detail.setText(str);
                tv_search.setVisibility(View.GONE);
                tv_gone_search.setVisibility(View.GONE);
                rl_search.setVisibility(View.VISIBLE);
                rl_gone_search.setVisibility(View.VISIBLE);
            } else {
                tv_search.setVisibility(View.VISIBLE);
                tv_gone_search.setVisibility(View.VISIBLE);
                rl_search.setVisibility(View.GONE);
                rl_gone_search.setVisibility(View.GONE);
            }
        } else {
            if (carSearch) {
                if (TextUtils.isEmpty(searchCar.origincityid)) {
                    tv_send_gone_ad.setText("全国");
                    tv_send_ad.setText("全国");
                } else {
                    tv_send_ad.setText(searchCar.originName);
                    tv_send_gone_ad.setText(searchCar.originName);
                }
                if (TextUtils.isEmpty(searchCar.receivecityid)) {
                    tv_end_ad.setText("全国");
                    tv_end_gone_ad.setText("全国");
                } else {
                    tv_end_ad.setText(searchCar.receiveName);
                    tv_end_gone_ad.setText(searchCar.receiveName);
                }

                StringBuilder stringBuilder = new StringBuilder();
                if (!TextUtils.isEmpty(searchCar.carName)) {
                    stringBuilder.append(searchCar.carName + ",");
                }
                if (!TextUtils.isEmpty(searchCar.longName)) {
                    stringBuilder.append(searchCar.longName + ",");
                }
                if (!TextUtils.isEmpty(searchCar.weightName)) {
                    stringBuilder.append(searchCar.weightName + ",");
                }
                if ("2".equals(searchCar.triptype)) {
                    stringBuilder.append("回程车");
                } else {
                    stringBuilder.append("直发车");
                }
                String str = stringBuilder.toString();
                if (str.endsWith(",")) {
                    str.substring(0, str.length() - 1);
                }
                tv_search_detail.setText(str);
                tv_search_gone_detail.setText(str);
                tv_search.setVisibility(View.GONE);
                tv_gone_search.setVisibility(View.GONE);
                rl_search.setVisibility(View.VISIBLE);
                rl_gone_search.setVisibility(View.VISIBLE);
            } else {
                tv_search.setVisibility(View.VISIBLE);
                tv_gone_search.setVisibility(View.VISIBLE);
                rl_search.setVisibility(View.GONE);
                rl_gone_search.setVisibility(View.GONE);
            }
        }
    }


    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == Activity.RESULT_OK) {
            if (requestCode == GOODS) {
                searchGoodsFinish();
                goodsSearch = true;
                searchGoods = (SearchGoods) data.getSerializableExtra(ExtraName.searchGoods);
            } else if (requestCode == CAR) {
                searchCarFinish();
                carSearch = true;
                searchCar = (SearchCar) data.getSerializableExtra(ExtraName.searchCar);
            }
            initSearchDetail();
        }
    }

    private void searchGoodsFinish() {
        goodsCursor = "0";
        goodsList.clear();
        headerListView.setPullLoadEnable(true);
        requestGoods(true);
    }


    private void searchCarFinish() {
        carCursor = "0";
        carList.clear();
        headerListView.setPullLoadEnable(true);
        requestCar(true);
    }

    private void requestMainImages() {
        RequestService.getInstance().mainIndex(getActivity(), 1 + "", MainEntity.class, new RequestListener() {

            @Override
            public void onSuccess(int requestCode, BaseEntity resultData) {

                if (resultData.isOk()) {
                    MainEntity result = (MainEntity) resultData;
                    AccountManager.saveBaoxian(result.getData().getBx());
                    setAdViewPager(result.getData().getFocus());
                } else {
                    ToastUtil.toast(resultData.getMsg());
                }
            }

            @Override
            public void onFailed(int requestCode, Exception error, String msg) {
                try {
                    ToastUtil.toast(R.string.please_check_network);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }


    private void setAdViewPager(List<FocusImage> images) {
        List<AdModel> dataList = new ArrayList<AdModel>();
        for (FocusImage focusImage : images) {

            if ("2".equals(focusImage.getPageid())) continue;//如果为 2 为电商图片

            AdModel aa = new AdModel();
            aa.setImageUrl(focusImage.getBigimg());
            aa.setLinkUrl(focusImage.getUrl());
            aa.setTitle(focusImage.getTitle());
            dataList.add(aa);
        }
        adViewPager.freshData(dataList);
//        scrollView.smoothScrollTo(0, 0);
    }

    private void requestCar(final boolean isLoading) {
        RequestService.getInstance().searchCar(getActivity(), isLoading ? ListViewAction.NEW : ListViewAction.HISTORY,
                size + "", carCursor, lng, lat, searchCar.cartype, searchCar.carcid, searchCar.longoption == 0 ? "" : searchCar.longoption + "", searchCar.weightoption == 0 ? "" : searchCar.weightoption + "",
                searchCar.origincityid, searchCar.receivecityid, searchCar.triptype,
                CarListEntity.class, new RequestListener() {
                    @Override
                    public void onSuccess(int requestCode, BaseEntity resultData) {
                        if (resultData.isOk()) {
                            CarListEntity entity = (CarListEntity) resultData;
                            List<Car> list = entity.getData().getList();
                            carList.addAll(list);
                            if ((carList.size() < size) || (!isLoading && list.size() < size)) {
                                headerListView.endLoadingMore("没有更多了");
                            }
                            setAdapter();
                        } else {
//                            showToast(resultData.getMsg());
                        }
                        onLoad();
                    }

                    @Override
                    public void onFailed(int requestCode, Exception error, String msg) {
//                        showToast(R.string.please_check_network);
                        onLoad();
                    }
                });
    }

    protected void setAdapter() {
        if (headerListAdapter == null) {
            headerListAdapter = new HeaderListAdapter(getActivity(), goodsList, carList, tabView, lat, lng);
            headerListView.setAdapter(headerListAdapter);
        } else {
            headerListAdapter.notifyData(listType);
        }
    }

    private void requestGoods(final boolean isLoading) {
        RequestService.getInstance().searchCargo(getActivity(), isLoading ? ListViewAction.NEW : ListViewAction.HISTORY, size + "", goodsCursor, lng, lat, searchGoods.shipptype + "", searchGoods.ranktype + "",
                searchGoods.origincityid, searchGoods.receivecityid, false, CargoListEntity.class, new RequestListener() {
                    @Override
                    public void onSuccess(int requestCode, BaseEntity resultData) {
                        if (resultData.isOk()) {
                            CargoListEntity entity = (CargoListEntity) resultData;
                            CargoListEntity.CargoList data = entity.getData();
                            List<Cargo> list = data.getList();
                            goodsList.addAll(list);
                            if ((goodsList.size() < size) || (!isLoading && list.size() < size)) {
                                headerListView.endLoadingMore("没有更多了");
                            }
                            setAdapter();

                        } else {
                        }
                        onLoad();

                    }

                    @Override
                    public void onFailed(int requestCode, Exception error, String msg) {
                        onLoad();
                    }
                });
    }

    @Override
    public void onRefresh() {

    }


    @Override
    public void onLoadMore() {
        if (listType == GOODS) {
            if (goodsList.size() > 0) {
                goodsCursor = goodsList.get(goodsList.size() - 1).getCursor();
            } else {
                goodsCursor = "0";
            }
            requestGoods(false);
        } else {
            if (carList.size() > 0) {
                carCursor = carList.get(carList.size() - 1).getCursor();
            } else {
                carCursor = "0";
            }
            requestCar(false);
        }

    }

    private void onLoad() {
        headerListView.stopRefresh();
        headerListView.stopLoadMore();
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        if (position == 0) return;
        if (listType == GOODS) {
            Intent intent = new Intent(getActivity(), GrabOrderActivity.class);
            intent.putExtra(ExtraName.orderid, goodsList.get(position - 3).getOrderid());
            startActivity(intent);
        } else {
            Intent intent = new Intent(getActivity(), CarOwnerInfoActivity.class);
            intent.putExtra(ExtraName.caruserid, carList.get(position - 3).getCaruserid());
            startActivity(intent);
        }
    }


    /* (non-Javadoc)
     * @see android.support.v4.app.Fragment#onResume()
     */
    @Override
    public void onResume() {
        mTopbarView.setCenterModel(TopbarView.ID_TITLE_INDEX_SHIPPER);
        super.onResume();
        MobclickAgent.onPageStart("CarFragment");
    }

    @Override
    public void onPause() {
        super.onPause();
        MobclickAgent.onPageEnd("CarFragment");
    }
}
